package com.ltwc.lttv.api;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Encrypt {

    static String encrypt(String cnlid, String timeStr, String appVer, String guid, String platform) {
        /*cnlid = "2000203803";
        timeStr = "1702166501";
        appVer = "V1.0.0";
        guid = "lq1y36mb_ccfwmja9zan";
        platform = "5910204";*/
        String  sr = "mg3c3b04ba";
        //|%s|%s|%s|%s|%s|%s|https://www.yangshipin.c|mozilla/5.0 (macintosh; ||Mozilla|Nets cape|MacIntel|
        //https://w.yangshipin.cn/|mozilla/5.0 (macintosh; ||Mozilla|Netscape|MacIntel|
        String ending = "https://www.yangshipin.c|mozilla/5.0 (macintosh; ||Mozilla|Nets cape|MacIntel|";

        // 1.拼接字符串
        String[] data_list = {"", cnlid, timeStr,sr, appVer, guid, platform, ending};
        String data_string = String.join("|", data_list);

        // 根据data_string生成qa
        int qn = createQn(data_string);

        // 待加密的明文
        String encrypt_string = qn + "|" + data_string;

        // 2.AES加密
        String ckey = "--01" + aesEncrypt(encrypt_string).toUpperCase();
        System.out.println("加密后的ckey--------->" + ckey);
        return ckey;
    }

    public static String aesEncrypt(String encryptString) {

        String keyHex = "4E2918885FD98109869D14E0231A0BF4";
        String ivHex = "16B17E519DDD0CE5B79D7A63A4DD801C";

        byte[] keyBytes = hexStringToByteArray(keyHex);
        byte[] ivBytes = hexStringToByteArray(ivHex);

        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        }
        try {
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        } catch (InvalidAlgorithmParameterException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }

        byte[] encryptedBytes = new byte[0];
        try {
            encryptedBytes = cipher.doFinal(encryptString.getBytes());
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        }
        return byteArrayToHexString(encryptedBytes);
    }


    public static byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));
        }
        return data;
    }

    private static String byteArrayToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X", b));
        }
        return sb.toString();
    }

    public static int createQn(String dataString) {
        int a = 0;
        for (int i = 0; i < dataString.length(); i++) {
            char _char = dataString.charAt(i);
            int charValue = (int) _char;
            a = (a << 5) - a + charValue;
            a &= a;
        }
        return a;
    }

    public static String hash(byte[] inputBytes){
        // 将字节数组转换为十六进制字符串
        StringBuilder sb = new StringBuilder();
        try {
            // 创建 MessageDigest 实例，指定算法为 MD5 SHA-256
            MessageDigest md = MessageDigest.getInstance("SHA-256");

            // 将输入字符串转换为字节数组
            //byte[] inputBytes = input.getBytes();

            // 更新 MessageDigest 实例的状态
            md.update(inputBytes);

            // 计算哈希值
            byte[] hashBytes = md.digest();


            for (byte b : hashBytes) {
                sb.append(String.format("%02x", b));
            }
            String md5Hash = sb.toString();

            // 输出 MD5 哈希值
            System.out.println("MD5 Hash: " + md5Hash);

        } catch (NoSuchAlgorithmException e) {
            // 捕获 NoSuchAlgorithmException 异常
            e.printStackTrace();
        }finally {

        }
        return sb.toString();
    }
}
